﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using OSSUtility;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// NumericKeyBoard.xaml 的交互逻辑
    /// </summary>
    public partial class NumericKeyBoard : Window
    {
        //nType 0:小数 1：整数
        static public bool ShowNumericKeyBoard(TextBox textBox, int nType = 0)
        {

            NumericKeyBoard form = new NumericKeyBoard(textBox.Text, nType);
            form.ShowDialog();
            if (form.bOK)
            {
                textBox.Text = form.txtValue.Text;
                return true;
            }

            return false;
        }

        static public bool ShowNumericKeyBoard(out string valOut, string val, int nType = 0)
        {
            valOut = "";
            NumericKeyBoard form = new NumericKeyBoard(val, nType);
            form.ShowDialog();
            if (form.bOK)
            {
                valOut = form.txtValue.Text;
                return true;
            }

            return false;
        }

        public bool bOK = true;
        public NumericKeyBoard(string val, int nType = 0)
        {
            InitializeComponent();
            txtValue.Text = val;
            if (nType == 1)
                btnDot.IsEnabled = false;

            txtValue.Focus();
            txtValue.SelectAll();




        }

        private void button_click(object sender, RoutedEventArgs e)
        {
            if (txtValue.SelectionLength > 0)
            {
                txtValue.Text = txtValue.Text.Remove(txtValue.SelectionStart, txtValue.SelectionLength);
            }

            string tag = ((Button)sender).Tag.ToString();
            txtValue.Text = txtValue.Text + tag;
        }

        private void FunButton_click(object sender, RoutedEventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();
            if (tag == "back")
            {
                if (txtValue.Text != "")
                {
                    if (txtValue.SelectionLength > 0)
                    {
                        txtValue.Text = txtValue.Text.Remove(txtValue.SelectionStart, txtValue.SelectionLength);
                    }
                    else
                    {
                        txtValue.Text = txtValue.Text.Remove(txtValue.Text.Length - 1, 1);
                    }
                }
            }

            if (tag == "clear")
            {
                txtValue.Text = "";
            }
        }

        private void btnCommOK_Click(object sender, RoutedEventArgs e)
        {
            bOK = true;
            Close();
        }

        private void btnCommCancel_Click(object sender, RoutedEventArgs e)
        {
            bOK = false;
            Close();
        }
    }
}
